package com.ms.game.itembox.container;

import java.math.BigDecimal;

import com.ms.game.common.container.LevelInfoBinUtil;
import com.ms.game.invitefriend.InviteCodeProcessor;
import com.ms.game.itembox.Item;
import com.ms.game.itembox.ItemBox;
import com.ms.game.level.LevelChangeWay;
import com.ms.game.level.db.LevelLogModel;
import com.ms.player.Player;
import com.ms.player.PlayerAttributeType;
import com.ms.player.UpdateAttributeList;
import com.ms.service.error.ErrorCode;
import com.ms.service.module.zone.BasicZone;
import com.ms.service.module.zone.module.BasicZoneModule;
import com.xpec.c4.game.common.Helper;
import com.xpec.c4.game.datamanager.BaseDataInfo;
import com.xpec.c4.game.datamanager.agent.PlayerData;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.service.contexts.Contexts;
import com.xpec.c4.service.module.common.PlayerCommonInfoManager;

public class ItemLevelUpCard extends ItemBasic{
	
	/** 用掉卡片可以增加的等級*/
	private int upgradeLevel;
	
	
	public int getUpgradeLevel() {
		return upgradeLevel;
	}

	public void setUpgradeLevel(int upgradeLevel) {
		this.upgradeLevel = upgradeLevel;
	}


	@Override
	public int use(Player player, Item useItem, int useCount, GUID targetGuid, Object... object) {
		return 0;
	}

	@Override
	public int applyItemToDc(BasicZoneModule zoneModule, Player player, Item useItem, GUID orderId, Object... object) {
		
		int beforeLevel = player.getLevel();
		int afterLevel = beforeLevel + upgradeLevel;
		BigDecimal beforeExp = player.getExp();
		BigDecimal expChangeValue = LevelInfoBinUtil.getExpByLevel(beforeLevel, upgradeLevel);
		BigDecimal afterExp = Helper.add(beforeExp, expChangeValue);
		
		player.setLevel(afterLevel);
		player.setExp(afterExp);
		
		boolean canGetInviteCode = false;
		if(beforeLevel < 50 && afterLevel >=50){
			canGetInviteCode = true;
		}
		
		LevelLogModel logModel = new LevelLogModel();
		logModel.setOrderId(orderId.toString());
		logModel.setAccountId(player.getAccountId().toString());
		logModel.setPlayerId(player.getGuid().toString());
		logModel.setPlayerName(player.getNameWithNumber());
		logModel.setExpBeforeChange(beforeExp);
		logModel.setExpChangeValue(expChangeValue);
		logModel.setExpAfterChange(afterExp);
		logModel.setLevelBefore(beforeLevel);
		logModel.setLevelAfter(afterLevel);
		logModel.setChangeWay(LevelChangeWay.ADD_USE_ITEM.getId());
		logModel.setChangeWayDesc(LevelChangeWay.ADD_USE_ITEM.getDesc());
		
		UpdateAttributeList updateList = new UpdateAttributeList();
		updateList.addUpdateValue(PlayerAttributeType.LEVEL, player.getLevel());
		updateList.addUpdateValue(PlayerAttributeType.EXP, player.getExp());
		zoneModule.getPlayerDataAgent().updatePlayerDataToDC2(player, updateList, this, "updatePlayerDataToDcCallback", canGetInviteCode, logModel);
		
		return ErrorCode.SUCCESS;
	}

	
	public void updatePlayerDataToDcCallback(BaseDataInfo obj, Object... userObject) {
		
		PlayerData data = (PlayerData) obj;
		Player player = data.getPlayer();
		GUID playerId = player.getGuid();
		
		if(obj.getResultCode() == ErrorCode.SUCCESS){
			boolean canGetInviteCode = (Boolean) userObject[0];
			if(canGetInviteCode){ //可以取得邀請碼，要給他
				InviteCodeProcessor.addInviteCodeToPlayer(playerId.toString());
			}
			
			//寫紀錄
			LevelLogModel levelLogModel = (LevelLogModel)userObject[1];
			BasicZoneModule module = (BasicZoneModule)Contexts.getModule();
			module.saveDbLog(levelLogModel);
		}
		
		GUID sessionId = PlayerCommonInfoManager.getInstance().getSessionByPlayerGuid(playerId);
		if(sessionId == null){
			return;
		}
		
		BasicZoneModule zoneModule = (BasicZoneModule)Contexts.getModule();
		BasicZone basicZone = zoneModule.getZone();
		//移除block list
		basicZone.removeBlockList(playerId, null);
		
		//先把這份送給client
		UpdateAttributeList updateAttributeList = data.getUpdateAttrList();
		updateAttributeList.sendToClient(playerId, sessionId, player.getUserType());
		
		ItemBox.sendUseItemResponseToClient(sessionId, obj.getResultCode() , player.getAccountId(), player.getGuid());
	}
		
}
